<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>php make page list</title>
    <style type="text/CSS">
        .page a {
            color: #0000FF;
            text-decoration: none;
        }
        .page{color:#0000FF;}
    </style>
</head>
<body>

<table border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
        <th width="30" height="38" bgcolor="#E3E3E3" scope="col">文章标题</th>
        <th width="500" bgcolor="#E3E3E3" scope="col">内容</th>
        <th width="50" bgcolor="#E3E3E3" scope="col">回帖</th>
    </tr>
    <?php
    $me=$_GET['ad'];
    $link=mysqli_connect('localhost','root','','abcd');
    header("Content-type:text/html;charset=utf-8");
    $Page_size=2;
    $result=mysqli_query($link,"select * from message WHERE type=$me");
    $count = mysqli_num_rows($result);
    $page_count  = ceil($count/$Page_size);
    $init=1;
    $page_len=3;
    $max_p=$page_count;
    $pages=$page_count;
 //判断当前页码
 if(empty($_GET['page'])||$_GET['page']<0){
        $page=1;
    }else {
        $page=$_GET['page'];
}
 $offset=$Page_size*($page-1);
 $sql="select * from message WHERE type=$me limit $offset,$Page_size";
 $result=mysqli_query($link,$sql);
 while ($row=mysqli_fetch_array($result)) {
     ?>
     <tr>
         <td bgcolor="#E0EEE0" height="90" width="100"><div align="center">
                 <?php echo $row['title']?>
             </div></td>
         <td bgcolor="#E0EEE" width="200"><div align="center">
                 <?php echo $row['content']?>
             </div></td>
         <td width="50" bgcolor="#6d87ff"><a href="huitie.php?id=<?php echo $row['id']; ?>">回帖</a></td>
     </tr>
     <?php
 }
 $page_len = ($page_len%2)?$page_len:$pagelen+1;//页码个数
 $pageoffset = ($page_len-1)/2;//页码个数左右偏移量

 $key='<div class="page">';
 $key.="<span>$page/$pages</span>&nbsp;";   //第几页,共几页
 if($page!=1){
     $key.="<a href=\"".$_SERVER['PHP_SELF']."?ad=$me&&page=1\">第一页</a> ";    //第一页
     $key.="<a href=\"".$_SERVER['PHP_SELF']."?ad=$me&&page=".($page-1)."\">上一页</a>"; //上一页
}else {
     $key.="<a href='javascript:;'>第一页</a>";//第一页
     $key.="<a href='javascript:;'>上一页</a>"; //上一页
 }
    if($pages>$page_len){
     //如果当前页小于等于左偏移
     if($page<=$pageoffset){
         $init=1;
         $max_p = $page_len;
     }else{//如果当前页大于左偏移
         //如果当前页码加右偏移超出最大分页数
         if($page+$pageoffset>=$pages+1){
             $init = $pages-$page_len+1;
         }else{
             //左右偏移都存在时的计算
             $init = $page-$pageoffset;
             $max_p = $page+$pageoffset;
         }
      }
       }
       for($i=$init;$i<=$max_p;$i++){
          if($i==$page){
              $key.=' <span>'.$i.'</span>';
       } else {
              $key.=" <a href=\"".$_SERVER['PHP_SELF']."?ad=$me&&page=".$i."\">".$i."</a>";
      }
      }
if($page!=$pages){
      $key.=" <a href=\"".$_SERVER['PHP_SELF']."?ad=$me&&page=".($page+1)."\">下一页</a> ";//下一页
 $key.="<a href=\"".$_SERVER['PHP_SELF']."?ad=$me&&page={$pages}\">最后一页</a>"; //最后一页
 }else {
      $key.="<a href='javascript:;'>下一页</a>";//下一页
      $key.="<a href='javascript:;'>最后一页</a>"; //最后一页
  }
 $key.='</div>';
?>
</table>
<br/><br/>
<div align="center"><?php echo $key?></div>
<div style="position: absolute;top:250px;right: -50px">
    <p style="margin-right: 50px;border-radius: 60px 5px;background-color: beige;text-align: center;line-height: 50px;
        width: 100px;height: 100px;"><a href="fatie.php?ad=<?php echo $me;?>">快来发一<br/>条帖子吧</a></p>
</div>
</body>
</html>